//GameHUD
using UnityEngine;
using System.Collections;
//used to instantiate List
using System.Collections.Generic;

public class GameHUD : MonoBehaviour
{
	public GUISkin guiSkin;
	public int nativeVerticalResolution = 1200;
	
	//main decoration textures
	public Texture2D healthImage;
	public Vector2 healthImageOffset = new Vector2(0f, 0f);
	
	//Health pie chart
	public List<Texture2D> healthPieImages = new List<Texture2D>();
	public Vector2 healthPieImageOffset = new Vector2(10f, 147f);
	
	//The lives count is display in the health image as a text counter
	public Vector2 livesCountOffset = new Vector2(425, 160);
	
	//Fuel Cell decoration image on the right side
	//Change this before final version
	public Texture2D fuelCellsImage;
	public Vector2 fuelCellOffset = new Vector2(0, 0);
	
	//The text counter inside the fuel cell image
	public Vector2 fuelCellCountOffset = new Vector2(391, 161);
	
	private ThirdPersonStatus playerInfo;
	
	void Awake()
	{
		playerInfo = (ThirdPersonStatus)FindObjectOfType(typeof(ThirdPersonStatus));
		if(!playerInfo)
		{
			Debug.Log("No link to player's state manager.");
		}
	}
	
	public void OnGui()
	{
		int itemsLeft = playerInfo.GetRemainingItems();
		
		int healthPieIndex = Mathf.Clamp(playerInfo.health, 0, healthPieImages.Count);
		
		if(itemsLeft < 0)
		{
			itemsLeft = 0;
		}
		
		//Setup GUI skin
		GUI.skin = guiSkin;
		
		//GUI is laid out in a 1920 x 1200 pixel display (16:10). 
		//Rescale to other resolutions without breaking HUD
		GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));
		
		//Health and Lives info
		DrawImageBottomAligned(healthImageOffset, healthImage);
		
		//Pie Chart
		//Graphics package used to check relative offsets and sizes
		Texture2D pieImage = healthPieImages[healthPieIndex -1];
		DrawImageBottomAligned(healthPieImageOffset, pieImage);
		
		//Displays lives as a number
		DrawLabelBottomAligned(livesCountOffset, playerInfo.lives.ToString());
		
		//Align fuel can/collectible items
		DrawImageBottomRightAligned(fuelCellOffset, fuelCellsImage);
		
		DrawLabelBottomRightAligned(fuelCellCountOffset, itemsLeft.ToString());
		
		
	}

	void DrawImageBottomAligned(Vector2 pos, Texture2D image)
	{
		GUI.Label(new Rect(pos.x, nativeVerticalResolution - image.height - pos.y, image.width, image.height), image);
	}	

	void DrawLabelBottomAligned( Vector2 pos, string text)
	{
		GUI.Label(new Rect(pos.x, nativeVerticalResolution - pos.y, 100, 100), text);
	}
		
	void DrawImageBottomRightAligned(Vector2 pos, Texture2D image)
	{
		int scaledResolutionWidth = nativeVerticalResolution / Screen.height * Screen.width;
		GUI.Label(new Rect(scaledResolutionWidth - pos.x - image.width, nativeVerticalResolution - image.height - pos.y, image.width, image.height), image);
	}

	void DrawLabelBottomRightAligned(Vector2 pos, string text)
	{
		int scaledResolutionWidth = nativeVerticalResolution / Screen.height * Screen.width;
		GUI.Label(new Rect (scaledResolutionWidth - pos.x, nativeVerticalResolution - pos.y, 100, 100), text);
	}

}